#include "card.h"


int Card::counter = 0;
Card *Card::previousCard;

Card::Card()
{
	button = new QPushButton();
	button->setIconSize(QSize(70,70));
	connect(button, SIGNAL( clicked() ),this, SLOT( turnCard() ));
}

Card::~Card()
{
	delete button;
}


	void Card::setReverse(QIcon icon)
	{
		reverse = icon;
	}

	QIcon Card::getReverse()
	{
		return reverse;
	}


	QIcon Card::getAverse()
	{
		return averse;
	}

	void Card::setAverse(QIcon icon)
	{
		averse = icon;
	}

	QPushButton* Card::getButton()
	{
		return button;
	}

	void Card::hideCards()
	{
		previousCard->button->hide();
		this->button->hide();
		counter=0;
	}

	void Card::reverseCards()
	{
		this->button->setIcon(reverse);
		previousCard->button->setIcon(reverse);
		this->button->blockSignals(0);
		previousCard->button->blockSignals(0);
		counter=0;
	}

	bool Card::compareIconTo(Card *card)
	{
		if (this->averse.pixmap(10,10).toImage() == previousCard->averse.pixmap(10,10).toImage())
			return true;
		else return false;
	}


	void Card::turnCard()
	{	
			counter++;
			if (counter==1)
				{
					this->button->setIcon(averse);
					previousCard = this;
					this->button->blockSignals(1);
				}
			else if (counter==2)
				{
					this->button->setIcon(averse);
					this->button->blockSignals(1);
					if (this->compareIconTo(previousCard)==true)
					{
						QTimer::singleShot(1000, this, SLOT(hideCards()));
					}
					else
					{
						QTimer::singleShot(1000, this, SLOT(reverseCards()));
					}
				}
				
	}
